import request from '@/utils/request'

// 获取权限列表
export function getPermissionList(params = {}) {
  return request({
    url: '/permissions',
    method: 'get',
    params: {
      page: params.page || 1,
      size: params.size || 10,
      search: params.search || ''
    }
  })
}

// 获取权限详情
export function getPermissionDetail(permissionId) {
  return request({
    url: `/permissions/${permissionId}`,
    method: 'get'
  })
}

// 新增权限
export function addPermission(data) {
  return request({
    url: '/permissions',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

// 更新权限
export function updatePermission(permissionId, data) {
  return request({
    url: `/permissions/${permissionId}`,
    method: 'put',
    data
  })
}

// 删除权限
export function deletePermission(permissionId) {
  return request({
    url: `/permissions/${permissionId}`,
    method: 'delete'
  })
}

// 启用权限
export function enablePermission(permissionId) {
  return request({
    url: `/permissions/${permissionId}/enable`,
    method: 'put'
  })
}

// 禁用权限
export function disablePermission(permissionId) {
  return request({
    url: `/permissions/${permissionId}/disable`,
    method: 'put'
  })
}

// 获取权限分页
export function getPermissionPage(obj) {
  return request({
    url: `/permissions?page=${obj.page}&size=${obj.size}`,
    method: 'get'
  })
}

// 分配权限到角色
export function assignPermissionToRole(roleId, permissionIds) {
  return request({
    url: `/roles/${roleId}/permissions`,
    method: 'post',
    data: { permissionIds }
  })
}

// 获取角色的权限列表
export function getRolePermissions(roleId) {
  return request({
    url: `/roles/${roleId}/permissions`,
    method: 'get'
  })
}

// 批量删除权限
export function batchDeletePermissions(permissionIds) {
  return request({
    url: '/permissions/batch',
    method: 'delete',
    data: { permissionIds }
  })
}

// 搜索权限
export function searchPermissions(params) {
  return request({
    url: '/permissions/search',
    method: 'get',
    params
  })
} 